622. 设计循环队列
为保证权益,题目请参考 622. 设计循环队列(From LeetCode).
解决方案1
Python
python
# 622. 设计循环队列
# https://leetcode.cn/problems/design-circular-queue/
class MyCircularQueue:
def __init__(self, k: int):
self.list = [0] * k
self.first_idx = 0
self.end_idx = 0
self.size = 0
self.max_size = k
self.k = k
def enQueue(self, value: int) -> bool:
if self.size == self.max_size:
return False
self.list[self.end_idx] = value
self.end_idx = (self.end_idx + 1) % self.k
self.size += 1
return True
def deQueue(self) -> bool:
if self.size == 0:
return False
self.first_idx = (self.first_idx + 1) % self.k
self.size -= 1
return True
def Front(self) -> int:
if self.size == 0:
return -1
return self.list[self.first_idx]
def Rear(self) -> int:
if self.size == 0:
return -1
return self.list[(self.end_idx + self.k - 1) % self.k]
def isEmpty(self) -> bool:
return self.size == 0
def isFull(self) -> bool:
return self.size == self.max_size
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47